package com.bdqn.BookManage.servlet;

import com.bdqn.BookManage.view.BookInfoView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : CP
 * @version: 1.0
 * @program : BookManage
 * @description : 显示所选的商品信息详情
 * @date : 2020-07-31 13:43
 **/
@WebServlet("/showCloseCart")
public class ShowCloseCartServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        /*  取出购物车 */
        Map<String, BookInfoView> cart = (Map<String, BookInfoView>) session.getAttribute("cart");
        List<BookInfoView> list = new ArrayList<>();

        for (BookInfoView bookInfoView: cart.values()) {
            if(bookInfoView.getIsSelected() == 1){
                /* 选中的商品 */
                list.add(bookInfoView);
            }
        }

        req.setAttribute("list",list);
        req.getRequestDispatcher("closeCart.jsp").forward(req,resp);

    }
}
